#include "event_app.h"
#include "sound_app.h"
#include "basic_app.h"
#include "ml307_app.h"
#include "tp1107_app.h"
#include "irq_contorl.h"
#include "battery_app.h"
#include "protocol_app.h"
#include "network_board.h"
#include "rs485_app.h"

#if _NETWOK_FUNCTION_ENABLE_

/**
 * @brief 配置管脚为低功耗模式
 *
 */
static void NetworkPinConfigLowPowerMode(void);

/**
 * @brief 配置管脚为正常模式
 *
 */
static void NetworkPinConfigNormalMode(void);

/**
 * @brief 无线模组管脚初始化
 *
 */
static void NetworkPinConfiguration(void);

/**
 * @brief 使能中断功能
 *
 */
static void NetworkEnableIrq(void);

/**
 * @brief 失能中断功能
 *
 */
static void NetworkDisableIrq(void);

/**
 * @brief 读取中断管脚电平
 *
 * @return bool - 管脚电平（true：高电平 false：低电平）
 */
static bool NetworkReadIrqnPinLevel(void);

/**
 * @brief 设置复位管脚为高电平
 *
 */
static void NetworkSetResetHighLevel(void);

/**
 * @brief 设置复位管脚为低电平
 *
 */
static void NetworkSetResetLowLevel(void);

/**
 * @brief 设置唤醒管脚为高电平
 *
 */
static void NetworkSetWakeupPinHighLevel(void);

/**
 * @brief 设置唤醒管脚为低电平
 *
 */
static void NetworkSetWakeupPinLowLevel(void);

/**
 * @brief 设置电源控制管脚为高电平
 *
 */
static void NetworkSetPowerContorHighLevel(void);

/**
 * @brief 设置电源控制管脚为低电平
 *
 */
static void NetworkSetPowerContorLowLevel(void);

/**
 * @brief 设置发送使能管脚为高电平
 *
 */
static void NetworkSetSendEnablePinHighLevel(void);

/**
 * @brief 设置发送使能管脚为低电平
 *
 */
static void NetworkSetSendEnablePinLowLevel(void);

/**
 * @brief 获取中断触发标志
 *
 * @return bool - 中断使能标志（true：触发 false：静默）
 */
static bool NetworkReadItFlagBit(void);

/**
 * @brief 清空中断标志
 *
 */
static void NetworkClearItFlagBit(void);

/**
 * @brief 串口发送
 *
 * @param pDataIn - 数据
 * @param SizeIn  - 数据发送长度
 */
static void NetworkUartSendString(uint8_t *pDataIn, uint16_t SizeIn);

/**
 * @brief 无线唤醒中断回调
 *
 * @return bool - 中断有效标志（true：有效 false：静默）
 */
static bool NetworkIrqTiggerCallback(void);

/**
 * @brief 无线模组事件处理回调
 *
 */
static void NetworkEventHandlerCallback(void);

/**
 * @brief 无线任务回调
 *
 */
static uint16_t NetworkEventTaskCallback(uint8_t TaskId, uint16_t Events);

static uint8_t NetworkEventTaskId = 0;
static bool NetworkIrqTiggerEn = false;
static bool NetworkDataReciveEn = false;
static IrqControl_t NetworkWakeupIrqControlNode = {0};
static EventHandlerTable_t NetworkEventHandlerTable = {0};
static NetworkDataPacket_t *NetworkMessageDataPacket = NULL;
static NetworkExternFunctionTable_t NetworkExternFunctionTable = {0};

static void NetworkPinConfigLowPowerMode(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ResetBits(NETWORK_DEVICE_IRQN_PIN);

  NetworkDisableIrq();
#endif

  R8_UART2_MCR = 0x00;
  R8_UART2_IER = 0x00;
  R8_UART2_FCR = 0x00;
  R8_UART2_LCR = 0x00;
  R8_UART2_IIR = 0x01;
  R8_UART2_LSR = 0x60;
  R8_UART2_RFC = 0x00;
  R8_UART2_TFC = 0x00;
  UART2_INTCfg(DISABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_DisableIRQ(UART2_IRQn);

  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  GPIOB_ResetBits(NETWORK_UART_RXD_PIN);
  GPIOB_ResetBits(NETWORK_UART_TXD_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOA_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeOut_PP_5mA);
  GPIOA_ResetBits(NETWORK_DEVICE_IRQN_PIN);

  NetworkDisableIrq();

#endif

  R8_UART3_MCR = 0x00;
  R8_UART3_IER = 0x00;
  R8_UART3_FCR = 0x00;
  R8_UART3_LCR = 0x00;
  R8_UART3_IIR = 0x01;
  R8_UART3_LSR = 0x60;
  R8_UART3_RFC = 0x00;
  R8_UART3_TFC = 0x00;
  UART3_INTCfg(DISABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_DisableIRQ(UART3_IRQn);

  GPIOA_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeOut_PP_5mA);
  GPIOA_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  GPIOA_ResetBits(NETWORK_UART_RXD_PIN);
  GPIOA_ResetBits(NETWORK_UART_TXD_PIN);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  R8_UART0_MCR = 0x00;
  R8_UART0_IER = 0x00;
  R8_UART0_FCR = 0x00;
  R8_UART0_LCR = 0x00;
  R8_UART0_IIR = 0x01;
  R8_UART0_LSR = 0x60;
  R8_UART0_RFC = 0x00;
  R8_UART0_TFC = 0x00;
  UART0_INTCfg(DISABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_DisableIRQ(UART0_IRQn);

  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeOut_PP_20mA);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_20mA);

  GPIOB_ResetBits(NETWORK_UART_RXD_PIN);
  GPIOB_ResetBits(NETWORK_UART_TXD_PIN);
#elif _BOARD_TYPE_ == _V33_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOA_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeOut_PP_5mA);
  GPIOA_ResetBits(NETWORK_DEVICE_IRQN_PIN);

  NetworkDisableIrq();
#endif

  R8_UART0_MCR = 0x00;
  R8_UART0_IER = 0x00;
  R8_UART0_FCR = 0x00;
  R8_UART0_LCR = 0x00;
  R8_UART0_IIR = 0x01;
  R8_UART0_LSR = 0x60;
  R8_UART0_RFC = 0x00;
  R8_UART0_TFC = 0x00;
  UART0_INTCfg(DISABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_DisableIRQ(UART0_IRQn);

  GPIOPinRemap(DISABLE, RB_PIN_UART0);
  GPIOA_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeOut_PP_5mA);
  GPIOA_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  GPIOA_ResetBits(NETWORK_UART_RXD_PIN);
  GPIOA_ResetBits(NETWORK_UART_TXD_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ResetBits(NETWORK_DEVICE_IRQN_PIN);

  NetworkDisableIrq();
#endif

  R8_UART0_MCR = 0x00;
  R8_UART0_IER = 0x00;
  R8_UART0_FCR = 0x00;
  R8_UART0_LCR = 0x00;
  R8_UART0_IIR = 0x01;
  R8_UART0_LSR = 0x60;
  R8_UART0_RFC = 0x00;
  R8_UART0_TFC = 0x00;
  UART0_INTCfg(DISABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_DisableIRQ(UART0_IRQn);

  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  GPIOB_ResetBits(NETWORK_UART_RXD_PIN);
  GPIOB_ResetBits(NETWORK_UART_TXD_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ResetBits(NETWORK_DEVICE_IRQN_PIN);

  NetworkDisableIrq();
#endif

  R8_UART2_MCR = 0x00;
  R8_UART2_IER = 0x00;
  R8_UART2_FCR = 0x00;
  R8_UART2_LCR = 0x00;
  R8_UART2_IIR = 0x01;
  R8_UART2_LSR = 0x60;
  R8_UART2_RFC = 0x00;
  R8_UART2_TFC = 0x00;
  UART2_INTCfg(DISABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_DisableIRQ(UART2_IRQn);

  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  GPIOB_ResetBits(NETWORK_UART_RXD_PIN);
  GPIOB_ResetBits(NETWORK_UART_TXD_PIN);
#endif
}

static void NetworkPinConfigNormalMode(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);

  NetworkEnableIrq();
#endif

  GPIOPinRemap(ENABLE, RB_PIN_UART2);
  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART2_DefInit();
  UART2_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART2_ByteTrigCfg(UART_7BYTE_TRIG);
  UART2_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART2_IRQn);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOA_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOA_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);

  NetworkEnableIrq();
#endif

  GPIOA_SetBits(NETWORK_UART_TXD_PIN);
  GPIOA_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOA_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART3_DefInit();
  UART3_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART3_ByteTrigCfg(UART_7BYTE_TRIG);
  UART3_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART3_IRQn);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_ModeCfg(NETWORK_ENABLE_PIN, GPIO_ModeOut_PP_20mA);

  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_20mA);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOA_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOA_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);

  NetworkEnableIrq();
#endif

  GPIOPinRemap(ENABLE, RB_PIN_UART0);
  GPIOA_SetBits(NETWORK_UART_TXD_PIN);
  GPIOA_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOA_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);

  NetworkEnableIrq();
#endif

  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);

  NetworkEnableIrq();
#endif

  GPIOPinRemap(ENABLE, RB_PIN_UART2);
  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART2_DefInit();
  UART2_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART2_ByteTrigCfg(UART_7BYTE_TRIG);
  UART2_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART2_IRQn);

#endif
}

static void NetworkPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_B_IRQn);

  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
#endif

  GPIOB_ModeCfg(NETWORK_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_DEVICE_WAKEUP_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_POWER_CONTORL_PIN, GPIO_ModeOut_PP_5mA);

  GPIOPinRemap(ENABLE, RB_PIN_UART2);
  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART2_DefInit();
  UART2_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART2_ByteTrigCfg(UART_7BYTE_TRIG);
  UART2_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART2_IRQn);

  NetworkSetResetPinLevel(true);
  NetworkSetWakeupPinLevel(true);
  NetworkSetPowerControlPinLevel(true);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOA_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOA_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_A_IRQn);

  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
#endif

  GPIOA_ModeCfg(NETWORK_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_DEVICE_WAKEUP_PIN, GPIO_ModeOut_PP_5mA);
  GPIOA_ModeCfg(NETWORK_POWER_CONTORL_PIN, GPIO_ModeOut_PP_5mA);

  GPIOA_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOPinRemap(ENABLE, RB_PIN_UART0);
  GPIOA_SetBits(NETWORK_UART_TXD_PIN);
  GPIOA_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);

  NetworkSetResetPinLevel(true);
  NetworkSetWakeupPinLevel(true);
  NetworkSetPowerControlPinLevel(true);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_ModeCfg(NETWORK_ENABLE_PIN, GPIO_ModeOut_PP_20mA);

  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_20mA);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);

  NetworkSetSendEnablePinLevel(false);
#elif _BOARD_TYPE_ == _CR01_BU_ // 485 //ok
  GPIOA_ModeCfg(NETWORK_ENABLE_PIN, GPIO_ModeOut_PP_20mA);
  
  GPIOA_SetBits(NETWORK_UART_TXD_PIN);
  GPIOA_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOA_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_20mA);
  GPIOPinRemap(ENABLE, RB_PIN_UART0);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);
  NetworkSetSendEnablePinLevel(false);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOA_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOA_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_A_IRQn);

  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
#endif

  GPIOA_ModeCfg(NETWORK_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);
  GPIOA_ModeCfg(NETWORK_DEVICE_WAKEUP_PIN, GPIO_ModeOut_PP_5mA);
  GPIOA_ModeCfg(NETWORK_POWER_CONTORL_PIN, GPIO_ModeOut_PP_5mA);

  GPIOPinRemap(ENABLE, RB_PIN_UART0);
  GPIOA_SetBits(NETWORK_UART_TXD_PIN);
  GPIOA_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOA_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);

  NetworkSetResetPinLevel(true);
  NetworkSetWakeupPinLevel(true);
  NetworkSetPowerControlPinLevel(true);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_B_IRQn);

  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
#endif

  GPIOB_ModeCfg(NETWORK_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_DEVICE_WAKEUP_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_POWER_CONTORL_PIN, GPIO_ModeOut_PP_5mA);

  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART0_DefInit();
  UART0_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART0_ByteTrigCfg(UART_7BYTE_TRIG);
  UART0_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART0_IRQn);

  NetworkSetResetPinLevel(true);
  NetworkSetWakeupPinLevel(true);
  NetworkSetPowerControlPinLevel(true);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  GPIOB_ModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(NETWORK_DEVICE_IRQN_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_B_IRQn);

  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
#endif

  GPIOB_ModeCfg(NETWORK_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_DEVICE_WAKEUP_PIN, GPIO_ModeOut_PP_5mA);
  GPIOB_ModeCfg(NETWORK_POWER_CONTORL_PIN, GPIO_ModeOut_PP_5mA);

  GPIOPinRemap(ENABLE, RB_PIN_UART2);
  GPIOB_SetBits(NETWORK_UART_TXD_PIN);
  GPIOB_ModeCfg(NETWORK_UART_RXD_PIN, GPIO_ModeIN_PU);
  GPIOB_ModeCfg(NETWORK_UART_TXD_PIN, GPIO_ModeOut_PP_5mA);

  UART2_DefInit();
  UART2_BaudRateCfg(NETWORK_UART_BAUD_RATE);
  UART2_ByteTrigCfg(UART_7BYTE_TRIG);
  UART2_INTCfg(ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT);
  PFIC_EnableIRQ(UART2_IRQn);

  NetworkSetResetPinLevel(true);
  NetworkSetWakeupPinLevel(true);
  NetworkSetPowerControlPinLevel(true);
#endif

  AddIrqNewTable(&NetworkWakeupIrqControlNode, NetworkIrqTiggerCallback);
}

static void NetworkEnableIrq(void)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_

#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PB_INT_EN |= NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PA_INT_EN |= NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PA_INT_EN |= NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PB_INT_EN |= NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _CR01_BU_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PB_INT_EN |= NETWORK_DEVICE_IRQN_PIN;
#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PB_INT_EN |= NETWORK_DEVICE_IRQN_PIN;
#endif

#endif
}

static void NetworkDisableIrq(void)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_

#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PB_INT_EN &= ~NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PA_INT_EN &= ~NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PA_INT_EN &= ~NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PB_INT_EN &= ~NETWORK_DEVICE_IRQN_PIN;

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
  R16_PB_INT_EN &= ~NETWORK_DEVICE_IRQN_PIN;
#endif

#endif
}

static bool NetworkReadIrqnPinLevel(void)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_

#if _BOARD_TYPE_ == _V31_A_BOARD_
  if (GPIOB_ReadPortPin(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  if (GPIOA_ReadPortPin(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  if (GPIOA_ReadPortPin(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  if (GPIOB_ReadPortPin(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  if (GPIOB_ReadPortPin(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;
#endif

#endif

  return false;
}

static void NetworkSetResetHighLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_SetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_SetBits(NETWORK_DEVICE_RESET_PIN);
#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_RESET_PIN);
#endif
}

static void NetworkSetResetLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_ResetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_ResetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ResetBits(NETWORK_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(NETWORK_DEVICE_RESET_PIN);
#endif
}

static void NetworkSetWakeupPinHighLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_SetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(NETWORK_DEVICE_WAKEUP_PIN);
#endif
}

static void NetworkSetWakeupPinLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_ResetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_ResetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ResetBits(NETWORK_DEVICE_WAKEUP_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(NETWORK_DEVICE_WAKEUP_PIN);
#endif
}

static void NetworkSetPowerContorHighLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_SetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_SetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_SetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(NETWORK_POWER_CONTORL_PIN);
#endif
}

static void NetworkSetPowerContorLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_ResetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_ResetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ResetBits(NETWORK_POWER_CONTORL_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(NETWORK_POWER_CONTORL_PIN);
#endif
}

static void NetworkSetSendEnablePinHighLevel(void)
{
#if _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_SetBits(NETWORK_ENABLE_PIN);
#elif _BOARD_TYPE_ == _CR01_BU_ // 485
  GPIOA_SetBits(NETWORK_ENABLE_PIN);//ok
#endif
}

static void NetworkSetSendEnablePinLowLevel(void)
{
#if _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_ResetBits(NETWORK_ENABLE_PIN);
#elif _BOARD_TYPE_ == _CR01_BU_ // 485
  GPIOA_ResetBits(NETWORK_ENABLE_PIN);//ok
#endif
}

static bool NetworkReadItFlagBit(void)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_

#if _BOARD_TYPE_ == _V31_A_BOARD_
  if (GPIOB_ReadITFlagBit(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  if (GPIOA_ReadITFlagBit(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  if (GPIOA_ReadITFlagBit(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  if (GPIOB_ReadITFlagBit(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  if (GPIOB_ReadITFlagBit(NETWORK_DEVICE_IRQN_PIN) == NETWORK_DEVICE_IRQN_PIN)
    return true;
#endif

#endif

  return false;
}

static void NetworkClearItFlagBit(void)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_

#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOA_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ClearITFlagBit(NETWORK_DEVICE_IRQN_PIN);
#endif

#endif
}

static void NetworkUartSendString(uint8_t *pDataIn, uint16_t SizeIn)
{
  PRINT("[network] : NetworkUartSendString: [\r\n");
  for (uint16_t i=0;i < SizeIn; i ++)
  {
    PRINT("%02x ", pDataIn[i]);
  }
  PRINT("]\r\n");
#if _BOARD_TYPE_ == _V31_A_BOARD_
  UART2_SendString(pDataIn, SizeIn);
  while ((R8_UART2_LSR & RB_LSR_TX_ALL_EMP) == 0)
  {
    __nop();
  }

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  UART3_SendString(pDataIn, SizeIn);
  while ((R8_UART3_LSR & RB_LSR_TX_ALL_EMP) == 0)
  {
    __nop();
  }

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  UART0_SendString(pDataIn, SizeIn);
  while ((R8_UART0_LSR & RB_LSR_TX_ALL_EMP) == 0)
  {
    __nop();
  }
#elif _BOARD_TYPE_ == _CR01_BU_ // 485
  UART0_SendString(pDataIn, SizeIn);
  while ((R8_UART0_LSR & RB_LSR_TX_ALL_EMP) == 0)
  {
    __nop();
  }

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  UART0_SendString(pDataIn, SizeIn);
  while ((R8_UART0_LSR & RB_LSR_TX_ALL_EMP) == 0)
  {
    __nop();
  }

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  UART0_SendString(pDataIn, SizeIn);
  while ((R8_UART0_LSR & RB_LSR_TX_ALL_EMP) == 0)
  {
    __nop();
  }

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  UART2_SendString(pDataIn, SizeIn);
  while ((R8_UART2_LSR & RB_LSR_TX_ALL_EMP) == 0)
  {
    __nop();
  }
#endif
}

static bool NetworkIrqTiggerCallback(void)
{
  if (NetworkReadItFlagBit() == true)
  {
    NetworkClearItFlagBit();

    NetworkIrqTiggerEn = true;
    return true;
  }

  return false;
}

static void NetworkEventHandlerCallback(void)
{
  if (NetworkIrqTiggerEn == true)
  {
    NetworkIrqTiggerEn = false;

    if (NetworkExternFunctionTable.AirborneAwakeningCallback != NULL)
    {
      NetworkExternFunctionTable.AirborneAwakeningCallback();
    }
  }

  if (NetworkDataReciveEn == true)
  {
    NetworkDataReciveEn = false;

    if (NetworkExternFunctionTable.UartRxDoneCallBack != NULL)
    {
      NetworkExternFunctionTable.UartRxDoneCallBack();
    }
  }
}

static uint16_t NetworkEventTaskCallback(uint8_t TaskId, uint16_t Events)
{
  if (TaskId == NetworkEventTaskId)
  {
    if (Events & NETWORK_COMMAND_OPERATION)
    {
      if (NetworkExternFunctionTable.CommandOperationCallback != NULL)
      {
        NetworkExternFunctionTable.CommandOperationCallback();
      }

      return (Events ^ NETWORK_COMMAND_OPERATION);
    }
  }

  if (TaskId == NetworkEventTaskId)
  {
    if (Events & NETWORK_MUTI_ELE_BOARD_COMU_CALLBACK)
    {
      if (NetworkExternFunctionTable.MutiEleBoardComuCallback != NULL)
      {
        NetworkExternFunctionTable.MutiEleBoardComuCallback();
      }

      return (Events ^ NETWORK_MUTI_ELE_BOARD_COMU_CALLBACK);
    }
  }

  return 0;
}

__INTERRUPT
__HIGH_CODE
#if _BOARD_TYPE_ == _V31_A_BOARD_
void UART2_IRQHandler(void)
{
  switch (UART2_GetITFlag())
  {
  case UART_II_LINE_STAT:
  {
    UART2_GetLinSTA(); // @suppress("Statement has no effect")
  }
  break;

  case UART_II_RECV_RDY:
  {
    uint8_t WirelessUartRxData = UART2_RecvByte();
    if (NetworkMessageDataPacket != NULL)
    {
      if (NetworkMessageDataPacket->DataSize < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize++] = WirelessUartRxData;
      }
    }
  }
  break;

  case UART_II_RECV_TOUT:
  {
    uint8_t TmpDataCount = 0, TmpDataBuff[8] = {0};

    TmpDataCount = UART2_RecvString(TmpDataBuff);
    if (NetworkMessageDataPacket != NULL)
    {
      if ((NetworkMessageDataPacket->DataSize + TmpDataCount) < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        memcpy(&NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize], TmpDataBuff, TmpDataCount);
        NetworkMessageDataPacket->DataSize += TmpDataCount;
      }
    }

    NetworkDataReciveEn = true;
  }
  break;
  }
}
#elif _BOARD_TYPE_ == _CR01_BU_
void UART0_IRQHandler(void)
{
  switch (UART0_GetITFlag())
  {
  case UART_II_LINE_STAT:
  {
    UART0_GetLinSTA(); // @suppress("Statement has no effect")
  }
  break;

  case UART_II_RECV_RDY:
  {
    uint8_t WirelessUartRxData = UART0_RecvByte();
    if (NetworkMessageDataPacket != NULL)
    {
      if (NetworkMessageDataPacket->DataSize < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize++] = WirelessUartRxData;
      }
    }
  }
  break;

  case UART_II_RECV_TOUT:
  {
    uint8_t TmpDataCount = 0, TmpDataBuff[8] = {0};

    TmpDataCount = UART0_RecvString(TmpDataBuff);
    if (NetworkMessageDataPacket != NULL)
    {
      //if ((NetworkMessageDataPacket->DataSize + TmpDataCount) < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        memcpy(&NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize], TmpDataBuff, TmpDataCount);
        NetworkMessageDataPacket->DataSize += TmpDataCount;
      }
    }

    NetworkDataReciveEn = true;
  }
  break;
  }
}

#elif _BOARD_TYPE_ == _V32_A_BOARD_
void UART3_IRQHandler(void)
{
  switch (UART3_GetITFlag())
  {
  case UART_II_LINE_STAT:
  {
    UART3_GetLinSTA(); // @suppress("Statement has no effect")
  }
  break;

  case UART_II_RECV_RDY:
  {
    uint8_t WirelessUartRxData = UART3_RecvByte();
    if (NetworkMessageDataPacket != NULL)
    {
      if (NetworkMessageDataPacket->DataSize < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize++] = WirelessUartRxData;
      }
    }
  }
  break;

  case UART_II_RECV_TOUT:
  {
    uint8_t TmpDataCount = 0, TmpDataBuff[8] = {0};

    TmpDataCount = UART3_RecvString(TmpDataBuff);
    if (NetworkMessageDataPacket != NULL)
    {
      if ((NetworkMessageDataPacket->DataSize + TmpDataCount) < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        memcpy(&NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize], TmpDataBuff, TmpDataCount);
        NetworkMessageDataPacket->DataSize += TmpDataCount;
      }
    }

    NetworkDataReciveEn = true;
  }
  break;
  }
}

#elif _BOARD_TYPE_ == _V32_B_BOARD_
void UART0_IRQHandler(void)
{
  switch (UART0_GetITFlag())
  {
  case UART_II_LINE_STAT:
  {
    UART0_GetLinSTA(); // @suppress("Statement has no effect")
  }
  break;

  case UART_II_RECV_RDY:
  {
    uint8_t WirelessUartRxData = UART0_RecvByte();
    if (NetworkMessageDataPacket != NULL)
    {
      if (NetworkMessageDataPacket->DataSize < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize++] = WirelessUartRxData;
      }
    }
  }
  break;

  case UART_II_RECV_TOUT:
  {
    uint8_t TmpDataCount = 0, TmpDataBuff[8] = {0};

    TmpDataCount = UART0_RecvString(TmpDataBuff);
    if (NetworkMessageDataPacket != NULL)
    {
      if ((NetworkMessageDataPacket->DataSize + TmpDataCount) < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        memcpy(&NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize], TmpDataBuff, TmpDataCount);
        NetworkMessageDataPacket->DataSize += TmpDataCount;
      }
    }

    NetworkDataReciveEn = true;
  }
  break;
  }
}

#elif _BOARD_TYPE_ == _V33_A_BOARD_
void UART0_IRQHandler(void)
{
  switch (UART0_GetITFlag())
  {
  case UART_II_LINE_STAT:
  {
    UART0_GetLinSTA(); // @suppress("Statement has no effect")
  }
  break;

  case UART_II_RECV_RDY:
  {
    uint8_t WirelessUartRxData = UART0_RecvByte();
    if (NetworkMessageDataPacket != NULL)
    {
      if (NetworkMessageDataPacket->DataSize < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize++] = WirelessUartRxData;
      }
    }
  }
  break;

  case UART_II_RECV_TOUT:
  {
    uint8_t TmpDataCount = 0, TmpDataBuff[8] = {0};

    TmpDataCount = UART0_RecvString(TmpDataBuff);
    if (NetworkMessageDataPacket != NULL)
    {
      if ((NetworkMessageDataPacket->DataSize + TmpDataCount) < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        memcpy(&NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize], TmpDataBuff, TmpDataCount);
        NetworkMessageDataPacket->DataSize += TmpDataCount;
      }
    }

    NetworkDataReciveEn = true;
  }
  break;
  }
}

#elif _BOARD_TYPE_ == _V33_B_BOARD_
void UART0_IRQHandler(void)
{
  switch (UART0_GetITFlag())
  {
  case UART_II_LINE_STAT:
  {
    UART0_GetLinSTA(); // @suppress("Statement has no effect")
  }
  break;

  case UART_II_RECV_RDY:
  {
    uint8_t WirelessUartRxData = UART0_RecvByte();
    if (NetworkMessageDataPacket != NULL)
    {
      if (NetworkMessageDataPacket->DataSize < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize++] = WirelessUartRxData;
      }
    }
  }
  break;

  case UART_II_RECV_TOUT:
  {
    uint8_t TmpDataCount = 0, TmpDataBuff[8] = {0};

    TmpDataCount = UART0_RecvString(TmpDataBuff);
    if (NetworkMessageDataPacket != NULL)
    {
      if ((NetworkMessageDataPacket->DataSize + TmpDataCount) < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        memcpy(&NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize], TmpDataBuff, TmpDataCount);
        NetworkMessageDataPacket->DataSize += TmpDataCount;
      }
    }

    NetworkDataReciveEn = true;
  }
  break;
  }
}

#elif _BOARD_TYPE_ == _V33_C_BOARD_
void UART2_IRQHandler(void)
{
  switch (UART2_GetITFlag())
  {
  case UART_II_LINE_STAT:
  {
    UART2_GetLinSTA(); // @suppress("Statement has no effect")
  }
  break;

  case UART_II_RECV_RDY:
  {
    uint8_t WirelessUartRxData = UART2_RecvByte();
    if (NetworkMessageDataPacket != NULL)
    {
      if (NetworkMessageDataPacket->DataSize < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize++] = WirelessUartRxData;
      }
    }
  }
  break;

  case UART_II_RECV_TOUT:
  {
    uint8_t TmpDataCount = 0, TmpDataBuff[8] = {0};

    TmpDataCount = UART2_RecvString(TmpDataBuff);
    if (NetworkMessageDataPacket != NULL)
    {
      if ((NetworkMessageDataPacket->DataSize + TmpDataCount) < NETWORK_MESSAGE_PACKET_LENGTH)
      {
        memcpy(&NetworkMessageDataPacket->DataBuff[NetworkMessageDataPacket->DataSize], TmpDataBuff, TmpDataCount);
        NetworkMessageDataPacket->DataSize += TmpDataCount;
      }
    }

    NetworkDataReciveEn = true;
  }
  break;
  }
}

#endif

void NetworkDelayMs(uint16_t Ms)
{
  DelayMs(Ms);
}

void NetworkSetResetPinLevel(bool PinLevel)
{
  (PinLevel == true) ? NetworkSetResetHighLevel() : NetworkSetResetLowLevel();
}

void NetworkSetWakeupPinLevel(bool PinLevel)
{
  (PinLevel == true) ? NetworkSetWakeupPinHighLevel() : NetworkSetWakeupPinLowLevel();
}

void NetworkSetPowerControlPinLevel(bool PinLevel)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  (PinLevel == true) ? NetworkSetPowerContorHighLevel() : NetworkSetPowerContorLowLevel();

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
  (PinLevel != true) ? NetworkSetPowerContorHighLevel() : NetworkSetPowerContorLowLevel();
#endif
}

void NetworkSetSendEnablePinLevel(bool PinLevel)
{
  (PinLevel == true) ? NetworkSetSendEnablePinHighLevel() : NetworkSetSendEnablePinLowLevel();
  NetworkDelayMs(NETWORK_WAIT_DEVICE_READY_TIME);
}

bool NetworkGetIrqnPinLevel(void)
{
  return NetworkReadIrqnPinLevel();
}

void NetworkEnableInterrupts(void)
{
  NetworkEnableIrq();
}

void NetworkDisableInterrupts(void)
{
  NetworkDisableIrq();
}

void NetworkHardwareReset(void)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  NetworkSetResetPinLevel(false);
  NetworkDelayMs(NETWORK_WAIT_DEVICE_READY_TIME);
  NetworkSetResetPinLevel(true);

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
  NetworkSetResetPinLevel(true);
  NetworkDelayMs(NETWORK_WAIT_DEVICE_READY_TIME);
  NetworkSetResetPinLevel(false);
#endif
}

bool NetworkUartSendData(const uint8_t *pDataIn, uint16_t SizeIn)
{
  uint8_t *pSendData = (uint8_t *)pDataIn;

  if (pDataIn != NULL)
  {
    if (SizeIn > 0)
    {
      NetworkUartSendString(pSendData, SizeIn);
      return true;
    }
  }

  return false;
}

uint8_t NetworkAuthenticationMessageType(const void *pDataIn, uint16_t DataSize)
{
  uint8_t MessageType = false;

  if (pDataIn != NULL)
  {
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
    MessageType = Tp1107ParsingIncomingMessageValidity(pDataIn, DataSize);

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
    MessageType = Ml307ParsingIncomingMessageValidity(pDataIn, DataSize);
#endif
  }

  return MessageType;
}

void NetworkInitiateDataTransfer(void)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  Tp1107CommunicationsInitialization();

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
  Ml307CommunicationsInitialization();
#endif
}

void NetworkDetermineIfKeyword(const void *pMessagePacket, uint16_t *pMessageSize)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  Tp1107FilterKeywords(pMessagePacket, pMessageSize);

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
  Ml307FilterKeywords(pMessagePacket, pMessageSize);
#endif
}

void NetWorkSendDataToHost(const uint8_t *pDataIn, uint16_t DataSize)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  NetworkUartSendData(pDataIn, DataSize);

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
  Ml307SendDataToServer(pDataIn, DataSize, NetworkUartSendData);

#elif _NETWOK_MODLE_TYPE_ == _RS485_MODULE_
  NetworkUartSendData(pDataIn, DataSize);
#endif
}

void NetworkForwardIncomingData(const uint8_t *pMessagePacket, uint16_t MessageSize)
{
#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  ProtocolPraseMessage(SOURCE_NETWORK_TRANSMISSION, pMessagePacket, MessageSize);

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
  Ml307ParsingMqttTopicPushMessages(pMessagePacket, MessageSize);
#endif
}


void NetworkNotificationSelfTestResult(bool Result)
{
  EventMarkingModuleSelfTestResults(NETWORK_MODULE_DEV_SELF_TEST, Result);
}

void NetworkNotificationNetworkAttachmentResult(NetworkNetAccessStatus_t AccessStatus)
{
  switch (AccessStatus)
  {
  case NETWORK_NET_ACCESSING:
    EventSetNetworkConnectStatus(NETWORK_LINKS_BEING_ESTABLISHED);
    break;

  case NETWORK_NET_ACCESS_ERROR:
    EventSetNetworkConnectStatus(NETWORK_CONNECT_ERROR);
    break;

  case NETWORK_NET_ACCESS_SUCCESS:
    EventSetNetworkConnectStatus(NETWORK_CONNECT_SUCCESS);
    break;

  default:
    EventSetNetworkConnectStatus(NETWORK_CLOSED);
    break;
  }
}

void NetworkNotificationResetResults(bool Result)
{
  EventMarkingModuleResetResult(DELETE_NETWORK_MODULE_DATA, Result);
}

void NetworkCommunicationHasBeenDisconnected(void)
{
  PRINT("[network]: disconnected.\r\n");
  EventNetworkDisconnect();
}

bool NetworkGetAttachmentStatus(void)
{
  if (EventGetNetworkConnectStatus() == NETWORK_CONNECT_SUCCESS)
    return true;

  return false;
}

void NetworkStartKeepAlive(void)
{
  EventStartWirelessWakeupTask();
}

bool NetworkDetermineIfRemoteWakeupIsSupported(void)
{
  if (_NETWOK_MODLE_TYPE_ == _TP1107_MODULE_)
    return true;

  return false;
}

bool NetworkDetermineIfThePowerIsWithinTheAllowableRange(void)
{
  if (BatteryGetVoltagePercentage() > NETWORK_LOW_POWER_PROTECTION_THRESHOLD)
    return true;

  return false;
}

void NetworkStopCommandOperationTask(void)
{
  tmos_stop_task(NetworkEventTaskId, NETWORK_COMMAND_OPERATION);
}

void NetworkDelayedStartCommandOperationTask(uint32_t Ms)
{
  tmos_stop_task(NetworkEventTaskId, NETWORK_COMMAND_OPERATION);
  tmos_start_task(NetworkEventTaskId, NETWORK_COMMAND_OPERATION, MS1_TO_SYSTEM_TIME(Ms));
}

void NetworkStartCommandOperationTaskNoWait(void)
{
  tmos_stop_task(NetworkEventTaskId, NETWORK_COMMAND_OPERATION);
  tmos_set_event(NetworkEventTaskId, NETWORK_COMMAND_OPERATION);
}

void NetworkStopMutiEleBoardComuCallbackTask(void)
{
  tmos_stop_task(NetworkEventTaskId, NETWORK_MUTI_ELE_BOARD_COMU_CALLBACK);
}

void NetworkDelayedStartMutiEleBoardComuCallbackTask(uint32_t Ms)
{
  tmos_stop_task(NetworkEventTaskId, NETWORK_MUTI_ELE_BOARD_COMU_CALLBACK);
  tmos_start_task(NetworkEventTaskId, NETWORK_MUTI_ELE_BOARD_COMU_CALLBACK, MS1_TO_SYSTEM_TIME(Ms));
}

void NetworkStartMutiEleBoardComuCallbackTaskNoWait(void)
{
  tmos_stop_task(NetworkEventTaskId, NETWORK_MUTI_ELE_BOARD_COMU_CALLBACK);
  tmos_set_event(NetworkEventTaskId, NETWORK_MUTI_ELE_BOARD_COMU_CALLBACK);
}

void NetworkConfigurationLowPower(void)
{
  NetworkSetWakeupPinLevel(false);

  if ((NetworkGetAttachmentStatus() != true) ||
      (NetworkDetermineIfRemoteWakeupIsSupported() != true) ||
      (NetworkDetermineIfThePowerIsWithinTheAllowableRange() != true))
  {
    PRINT("[network]: turn off the main power.\r\n");

    NetworkDisableInterrupts();
    NetworkPinConfigLowPowerMode();
    NetworkSetResetPinLevel(false);
    NetworkSetPowerControlPinLevel(false);

    if (NetworkGetAttachmentStatus() == true)
    {
      NetworkNotificationNetworkAttachmentResult(NETWORK_NET_NO_USE);
    }
  }
}

void NetworkConfigurationNormalMode(void)
{
  PRINT("[network]: turn on the main power.\r\n");

  NetworkEnableInterrupts();
  NetworkPinConfigNormalMode();
  NetworkSetWakeupPinLevel(true);
  NetworkSetPowerControlPinLevel(true);
}

void NetworkBoardBoardInitialisation(NetworkTaskApi_t *pTaskApiOut, NetworkExternFunctionTable_t *pExternFunctionIn, NetworkDataPacket_t *pDataPacketIn)
{
  NetworkMessageDataPacket = pDataPacketIn;

  NetworkExternFunctionTable.UartRxDoneCallBack = pExternFunctionIn->UartRxDoneCallBack;
  NetworkExternFunctionTable.CommandOperationCallback = pExternFunctionIn->CommandOperationCallback;
  NetworkExternFunctionTable.AirborneAwakeningCallback = pExternFunctionIn->AirborneAwakeningCallback;
  NetworkExternFunctionTable.MutiEleBoardComuCallback = pExternFunctionIn->MutiEleBoardComuCallback;

#if _NETWOK_MODLE_TYPE_ == _TP1107_MODULE_
  pTaskApiOut->SelfTestCallback.Sending = Tp1107BuildSelfTestMessage;
  pTaskApiOut->SelfTestCallback.Parsing = Tp1107ParsingSelfTestMessage;
  pTaskApiOut->ResetSystemStatusCallback.Sending = Tp1107BuildDeviceResetMessage;
  pTaskApiOut->ResetSystemStatusCallback.Parsing = Tp1107ParsingDeviceResetMessage;
  pTaskApiOut->ForceExitCommandModeCallback.Sending = Tp1107BuildForceExitCommandModeMessage;
  pTaskApiOut->ForceExitCommandModeCallback.Parsing = Tp1107ParsingForceExitCommandModeMessage;
  pTaskApiOut->InitiateNetAttachmentCallback.Sending = Tp1107BuildInitiateNetAttachmentMessage;
  pTaskApiOut->InitiateNetAttachmentCallback.Parsing = Tp1107ParsingInitiateNetAttachmentMessage;

#elif _NETWOK_MODLE_TYPE_ == _ML307R_MODLE_
  pTaskApiOut->SelfTestCallback.Sending = Ml307BuildSelfTestMessage;
  pTaskApiOut->SelfTestCallback.Parsing = Ml307ParsingSelfTestMessage;
  pTaskApiOut->ResetSystemStatusCallback.Sending = NULL;
  pTaskApiOut->ResetSystemStatusCallback.Parsing = NULL;
  pTaskApiOut->ForceExitCommandModeCallback.Sending = NULL;
  pTaskApiOut->ForceExitCommandModeCallback.Parsing = NULL;
  pTaskApiOut->InitiateNetAttachmentCallback.Sending = Ml307BuildInitiateNetAttachmentMessage;
  pTaskApiOut->InitiateNetAttachmentCallback.Parsing = Ml307ParsingInitiateNetAttachmentMessage;

#elif _NETWOK_MODLE_TYPE_ == _RS485_MODULE_
  pTaskApiOut->SelfTestCallback.Sending = NULL;
  pTaskApiOut->SelfTestCallback.Parsing = NULL;
  pTaskApiOut->ResetSystemStatusCallback.Sending = NULL;
  pTaskApiOut->ResetSystemStatusCallback.Parsing = NULL;
  pTaskApiOut->ForceExitCommandModeCallback.Sending = NULL;
  pTaskApiOut->ForceExitCommandModeCallback.Parsing = NULL;
  pTaskApiOut->InitiateNetAttachmentCallback.Sending = NULL;
  pTaskApiOut->InitiateNetAttachmentCallback.Parsing = NULL;
  pTaskApiOut->OpenSpecificFloorCallback.Sending = Rs485BuildOpenSpecificFloorMessage;
  pTaskApiOut->OpenSpecificFloorCallback.Parsing = Rs485ParsingOpenSpecificFloorMessage;
  pTaskApiOut->ElevatorIoBoardFirmwareTransCallback.Sending = Rs485BuildIoBoardFirmwareTransMessage;
  pTaskApiOut->ElevatorIoBoardFirmwareTransCallback.Parsing = Rs485ParsingIoBoardFirmwareTransMessage;
  pTaskApiOut->GetVersionCallback.Sending = Rs485BuildGetVersionMessage;
  pTaskApiOut->GetVersionCallback.Parsing = Rs485ParsingGetVersionMessage;
  pTaskApiOut->OpenEmergencyModeCallback.Sending = Rs485BuildOpenEmergencyModeMessage;
  pTaskApiOut->OpenEmergencyModeCallback.Parsing = Rs485ParsingOpenEmergencyModeMessage;
  pTaskApiOut->ElevatorIoBoardQueryOtaStatusCallback.Sending = Rs485BuildQueryIoBoardFirmwareStatusMessage;
  pTaskApiOut->ElevatorIoBoardQueryOtaStatusCallback.Parsing = Rs485ParsingQueryIoBoardFirmwareStatusMessage;
  pTaskApiOut->QueryIoBoardMacAddressCallback.Sending = Rs485BuildQueryIoBoardMacAddressMessage;
  pTaskApiOut->QueryIoBoardMacAddressCallback.Parsing = Rs485ParsingQueryIoBoardMacAddressMessage;
  pTaskApiOut->SetBoardSelfTestModeCallback.Sending = Rs485BuildSetBoardSelfTestFrameMessage;
  pTaskApiOut->SetBoardSelfTestModeCallback.Parsing = Rs485ParsingSetBoardSelfTestFrameMessage;
  pTaskApiOut->SetBoardFireAlarmWorkModeCallback.Sending = Rs485BuildSetBoardFireAlarmWorkModeFrameMessage;
  pTaskApiOut->SetBoardFireAlarmWorkModeCallback.Parsing = Rs485ParsingSetBoardFireAlarmWorkModeFrameCallback;
  pTaskApiOut->QueryBoardFireAlarmStatusCallback.Sending = Rs485BuildQueryBoardFireAlarmStatusFrameMessage;
  pTaskApiOut->QueryBoardFireAlarmStatusCallback.Parsing = Rs485ParsingQueryBoardFireAlarmStatusFrameCallback;
#endif

  NetworkPinConfiguration();
  EventAddNewEventHandler(&NetworkEventHandlerTable, NetworkEventHandlerCallback);
  NetworkEventTaskId = TMOS_ProcessEventRegister(NetworkEventTaskCallback);
}

#endif
